package com.thkj.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.thkj.interceptor.Permissions;
import com.thkj.entity.RmEnum;
import com.thkj.entity.RmEnumDetail;
import com.thkj.service.RmEnumService;
import com.thkj.util.RequestUtil;

import dp.R;
import dp.dao.PagingList;
import com.thkj.framework.base.BaseController;

@RestController
@RequestMapping("/backstage/enum")
public class RmEnumAction extends BaseController {
	@Resource
    RmEnumService rmEnumService;
	
	@Permissions("enum_001")
	@RequestMapping("")
    public ModelAndView index(){
    	return new ModelAndView("enum");
    }
    
    @RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size){
    	Map<String, Object> param = RequestUtil.getParameterMap(request);
    	PagingList<RmEnum> list = rmEnumService.query(page, size,param);
    	return R.success(list);
    }
    
    @Permissions("enum_002")
    @RequestMapping("/edit")
    public R edit(String oldEnumCd, RmEnum rmEnum){
    	try {
    		if(StringUtils.isBlank(oldEnumCd)) {
        		rmEnumService.insert(rmEnum);
        	}else {
        		rmEnumService.update(oldEnumCd, rmEnum);
        	}
        	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @Permissions("enum_003")
    @RequestMapping("/delete")
    public R delete(String enumCd){
		rmEnumService.delete(enumCd);
    	return R.SUCCESS;
    }
    
    @RequestMapping("/getDetail")
    public R getDetail(String enumCd){
    	List<RmEnumDetail> list = rmEnumService.getDetail(enumCd, null);
    	return R.success(list);
    }
    
    @Permissions("enum_002")
    @RequestMapping("/saveDetail")
    public R saveDetail(RmEnumDetail detail){
    	try {
	    	if(StringUtils.isBlank(detail.getEnumDetailId())){
	    		rmEnumService.insertDetail(detail);
	    	}else {
	    		rmEnumService.updateDetail(detail);
	    	}
	    	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @Permissions("enum_003")
    @RequestMapping("/deleteDetail")
    public R deleteDetail(String enumDetailId){
    	rmEnumService.deleteDetail(enumDetailId);
    	return R.SUCCESS;
    }
    
    @RequestMapping("/getEanbledDetail")
    public R getEanbledDetail(String enumCd){
    	List<RmEnumDetail> list = rmEnumService.getDetail(enumCd, "1");
    	return R.success(list);
    }

    @RequestMapping("/queryEnumDetail")
    public R queryEnumDetail(String enumCd){
    	List<Map> list = rmEnumService.queryEnumDetail(enumCd, "1");
    	return R.success(list);
    }

}